#include "tcpServer.hpp"
#include <iostream>
#include <memory>


using namespace std;
using namespace Server;

bool heandler(const Request &res, Response &resp)
{
#ifdef DEBUG
    std::cout << "处理请求" << std::endl;
#endif

    switch (res._op)
    {
        case '+':
            resp._result = res._x + res._y;
            break;
        case '-':
            resp._result = res._x - res._y;
            break;
        case '*':
            resp._result = res._x * res._y;
            break;
        case '/':
            {
                if (res._y == 0)
                    resp._exitcode = DIV_ERRO;
                else
                    resp._result = res._x / res._y;
            }
            break;
        case '%':
            {
                if (res._y == 0)
                    resp._exitcode = MOD_ERRO;
                else
                    resp._result = res._x % res._y;
            }
            break;
        default:
            resp._exitcode = OPERA_ERRO;
            break;
    }
    return true;
}

inline void Usage(std::string proc)
{
    std::cout << "Usage : \n\t" << proc << " local_port\n\n";
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERROR);
    }

    uint16_t port = atoi(argv[1]);

    unique_ptr<TcpServer> tcpServer(new TcpServer());
    tcpServer->init();

    tcpServer->start(heandler);

    return 0;
}